package com.lk.mall.controller.admin;

import com.lk.mall.config.annotation.TokenToAdminUser;
import com.lk.mall.model.entity.AdminUserToken;
import com.lk.mall.model.entity.GoodsCategory;
import com.lk.mall.service.ICategoryService;
import com.lk.mall.util.PageQueryUtil;
import com.lk.mall.util.PageResult;
import com.lk.mall.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author cat
 * @description
 * @date 2022/9/15 8:48 PM
 */
@Api(value = "分类管理", tags = {"GoodsCategoryAPI"})
@Slf4j
@RestController
@RequestMapping("/manage-api/v1")
public class GoodsCategoryAPI {

    ICategoryService categoryService;

    public GoodsCategoryAPI(ICategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @GetMapping("/categories")
    @ApiOperation("商品分类列表")
    public R<PageResult<GoodsCategory>> list(@RequestParam(required = false) @ApiParam(value = "页码")Integer pageNumber,
                  @RequestParam(required = false) @ApiParam(value = "每页条数") Integer pageSize,
                  @RequestParam(required = false) @ApiParam(value = "分类级别") Integer categoryLevel,
                  @RequestParam(required = false) @ApiParam(value = "父级分类的ID") Long parentId,
                  @TokenToAdminUser AdminUserToken adminUserToken){

        log.info("adminUser:{}", adminUserToken.toString());

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("page", pageNumber);
        paramsMap.put("limit", pageSize);
        paramsMap.put("categoryLevel", categoryLevel);
        paramsMap.put("parentId", parentId);
        PageQueryUtil pageQueryUtil = new PageQueryUtil(paramsMap);

        PageResult<GoodsCategory> categoriesInfo = categoryService.getCategory(pageQueryUtil);
        return new R<PageResult<GoodsCategory>>().ok(categoriesInfo);
    }
}
